/**
 * 获取当前的时间
 * @return {string} {格式化后的当前时间}
 */
export function getCurrentDate(mode) {
	const current_date = new Date();

	const year = current_date.getFullYear();

	let months = current_date.getMonth() + 1;

	let date = current_date.getDate();

	if (months < 10) {
		months = `0${months}`;
	}

	if (date < 10) {
		date = `0${date}`;
	}

	if (mode) {
		switch (mode) {
		case 'year':

			return year;

		case 'y-m':

			return `${year}-${months}`;

		case 'month':

			return months;

		case 'day':

			return date;

		default:

			break;
		}
	}

	return `${year}-${months}-${date}`;
}

/**
 * 网址格式化
 * @return {Object} {格式化网址之后得到的参数对象}
 */
export function formaturl() {
	const url_info = {};

	const url_object = window.location.href.split('#').shift().split('?').pop();

	url_object.split('&').forEach((url) => {
		const key = url.split('=').shift();
		const value = url.split('=').pop();

		if (key) {
			url_info[key] = value;
		}
	});

	return url_info;
}

/**
 * 图片预加载
 * @param  {array} src_arrs {图片链接数组}
 * @return {array} {加载后的图片数组}
 */
export function imgPreLoad(src_arrs) {
	const imgs = [];

	src_arrs.forEach((src, index) => {
		const img = new Image();

		img.src = src;

		imgs[index] = img;
	});

	return imgs;
}

/**
 * 判断a数组是否包含b数组
 * @param  {array} a {判断的a数组}
 * @param  {array} b {判断的b数组}
 * @return {Boolean} {true代表包含，false代表不包含}
 */
export function isContained(a, b) {
	if (!(a instanceof Array) || !(b instanceof Array)) return false;

	if (a.length < b.length) return false;

	const aStr = a.toString();

	for (let i = 0, len = b.length; i < len; i += 1) {
		if (aStr.indexOf(b[i]) === -1) return false;
	}

	return true;
}


/**
 * 图片上传预览
 * @param  {file} file {图片文件}
 * @param  {Object} _image {$refs获取节点}
 */

export function getUploadImage(file, _image) {
	const reader = new FileReader();

	reader.onload = function (event) {
		_image.src = event.target.result;
	};

	reader.readAsDataURL(file);
}

/**
 * 去除对象中的空值
 * @param  {Object} param {需要进行处理的对象}
 * @return {Object} {去除完毕的对象}
 */
export function deleteEmptyValue(param) {
	const query_param = {};

	Object.keys(param).forEach((key) => {
		const value = param[key];
		if (value && value.length) {
			if (value.length > 0) {
				query_param[key] = value;
			}
		} else if (String(value)) {
			query_param[key] = value;
		}
	});

	return query_param;
}

/**
 *毫秒转化时分秒
 *@param {Number} date {需要转化的毫秒}
 *@return {undefined}
 */
export function conversionTime(date) {
	let dd = '';
	const hh = Math.floor(date / 3600);
	const hh_mod = `0${  hh % 24}`;

	if (hh > 24) {
		dd = `${Math.floor(hh / 24)  }d`;
	}
	const mm = `0${  Math.floor((date - (hh * 3600)) / 60)}`;
	const ss = `0${  Math.floor((date - (hh * 3600)) % 60)}`;

	const time = `${dd} ${hh_mod.slice(-2)}:${mm.slice(-2)}:${ss.slice(-2)}`;

	return time;
}

/**
 * 调整模态框的水平居中
 * @return {undefined}
 */
export function resizeModal() {
	const modals = [...document.querySelectorAll('.ivu-modal-wrap')];
	if (!modals || modals.length === 0) return;

	const side_menu = document.querySelector('.layout-side-menu');
	modals.forEach((modal) => {
		modal.style.paddingLeft = `${side_menu.offsetWidth}px`;
	});
}

/**
 * @param url
 * @return {boolean}
 */
export function isUrlValidate(url) {
	return /https?:\/\/\w+\.\w{2,}/.test(url);
}
